package org.akomantoso.core.data;

import java.io.IOException;
import java.util.Calendar;

import org.akomantoso.core.data.attribute.Time;
import org.akomantoso.core.data.group.ANinline;
import org.akomantoso.core.data.type.TimeType;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.OutputUtil;
import org.akomantoso.core.io.util.DateUtil;
import org.akomantoso.core.visitor.AKVisitor;

/**
 * The element recordedTime is an inline element for the specification of an explicit mention of a time (e.g., in a
 * debate).
 * 
 * <pre>
 * 	<xsd:element name="recordedTime">
 * 		<xsd:complexType mixed="true">
 * 			<xsd:complexContent>
 * 				<xsd:extension base="inline">
 * 					<xsd:attribute name="type" type="timeType"/>
 * 					<xsd:attributeGroup ref="time"/>
 * 				</xsd:extension>
 * 			</xsd:complexContent>
 * 		</xsd:complexType>
 * 	</xsd:element>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 */
public final class RecordedTime extends InlineType implements Time,ANinline {

	public static final String ELEMENT = "recordedTime";
	
	public static final String ATTRIBUTE_TYPE = "type";
	

	private TimeType _type;
	
	private Calendar _time;
	
	public TimeType getType() {
		return _type;
	}

	public void setType(TimeType type) {
		_type = type;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Time#getTime()
	 */
	@Override
	public Calendar getTime() {
		return _time;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Time#setTime(java.util.Calendar)
	 */
	@Override
	public void setTime(Calendar time) {
		_time = time;
	}

	/**
	 * @see org.akomantoso.core.data.InlineType#writeExternal(org.akomantoso.core.io.Output)
	 */
	@Override
	public <E extends Output> void writeExternal(E out) throws IOException {
		out.writeStart(ELEMENT);
		OutputUtil.writeAttribute(out, ATTRIBUTE_TYPE, _type);
		OutputUtil.writeTime(out, this);
		super.writeExternal(out);
		out.writeEnd();
	}


	
	/**
	 * @see org.akomantoso.core.data.InlineType#readExternal(org.akomantoso.core.io.Input)
	 */
	@Override
	public <E extends Input> void readExternal(E in) throws IOException {
		_type = TimeType.checkValueOf(in.readAttribute(ATTRIBUTE_TYPE));
		_time = DateUtil.convertTime(in.readAttribute(Time.ATTRIBUTE));
		super.readExternal(in);
	}

	/**
	 * @see org.akomantoso.core.visitor.AKVisitable#accept(org.akomantoso.core.visitor.AKVisitor)
	 */
	@Override
	public void accept(AKVisitor visitor) {
	}
	
	
}
